

from curl_cffi import requests
import re
from log.log import log
from log.header import get_headers
import os
import shutil
logger = log('../tu/log/',__file__.split('.')[-2])


d = '../tu/彼岸图网/'
if os.path.exists(d):
    shutil.rmtree(d)
    os.makedirs(d)
else:
    os.makedirs(d)

logger.info('开始爬取')
try:
    for i in range(1, 11):
        if i == 1:
            url_page = 'https://pic.netbian.com/new/index.html'
        else:
            url_page = f'https://pic.netbian.com/new/index_{i}.html'
        logger.info(f'正在爬取第{i}页信息-----')
        response = requests.get(url_page, headers=get_headers(r=url_page))
        items = re.findall(f'<a href="/tupian/(.*?).html" target="_blank"><img src="(.*?)" alt="(.*?)" />.*?</a>',
                           response.content.decode('gbk'), re.S)

        for item in items:
            detail_url = f'https://pic.netbian.com/tupian/{item[0]}.html'
            small_img_url = f'https://pic.netbian.com{item[1]}'
            title = item[2]

            res_detail_url = requests.get(detail_url, headers=get_headers(r=detail_url))

            re_detail_url = re.search(
                r'<div class="photo-pic"><a href="" id="img"><img src="(.*?)".*?title="(.*?)"></a></div>',
                res_detail_url.content.decode('gbk'), re.S)
            big_items = re_detail_url
            big_url = f'https://pic.netbian.com{big_items.group(1)}'
            big_title = big_items.group(2)

            # logger.info('开始爬取预览图====')
            # 获取预览图
            res_big_img = requests.get(big_url, headers=get_headers(r=detail_url))
            big_img = res_big_img.content
            with open(f'{d}{big_title}_big.jpg', 'wb') as f:
                f.write(big_img)

            # logger.info('开始爬取缩裂图====')
            # 获取缩裂图
            res_small_img = requests.get(small_img_url, headers=get_headers(r=url_page))
            small_img = res_small_img.content
            with open(f'{d}{title}_small.jpg', 'wb') as f:
                f.write(small_img)
except Exception as e:
    logger.error(e)

logger.info('爬取结束')